class Solution{
    vector<vector<int> > generateMatrix(int n) {
        int i = 0,j = 0,k = 1;
        vector<vector<int> > v(n, vector<int>(n, 0));
        int nl = 0, nh = n-1, ml = 0, mh = n-1;
        while(true){
            while(j<=nh) v[i][j++] = k++;
            j--;
            i++;
            ml++;
            if(ml>mh||nl>nh) return v;
            while(i<=mh) v[i++][j] = k++;
            i--;
            j--;
            nh--;
            if(ml>mh||nl>nh) return v;
            while(j>=nl) v[i][j--] = k++;
            j++;
            i--;
            mh--;
            if(ml>mh||nl>nh) return v;
            while(i>=ml) v[i--][j] = k++;
            i++;
            j++;
            nl++;
            if(ml>mh||nl>nh) return v;
        }
    }
};
